<?php

namespace app\api\controller\teams;

use app\common\func\Constant;
use think\Db;

/**
 * 团队
 * Class Terminal
 * @package app\api\controller\product
 */
class Teams
{
	/**
	 * 实例对象
	 * @var object
	 */
	protected static $instance;

	/**
	 * 默认配置
	 * @var array
	 */
	protected $config  = [
		"prefix" 	=> "fd_",
		"user" 		=> "user",
		"buy" 		=> "equip_buy",
		"order"		=> "order"
	];

	protected $buyMode = null;

	public function __construct()
	{
	}


	/**
	 * 实例化
	 */
	public static function instance()
	{
		if (is_null(self::$instance)) {
			self::$instance = new static();
		}
		return self::$instance;
	}

	/**
	 * 列表
	 */
	public function lists($uid, $page = 1)
	{
		if (empty($uid)) {
			return false;
		}
		$req = $this->under_data($uid, $page);
		if ($req) {
			foreach ($req as $k => $vals) {
				$ret = $this->total_data($vals["uid"]);
				$req[$k]["con"] = $ret["con"];
				$req[$k]["buy"] = $this->bus_sum($vals["uid"]);
				$req[$k]["order"] = $this->order_sum($vals["uid"]);
			}
		}
		return $req;
	}

	/**
	 * 总计
	 */
	public function total_data($uid)
	{
		$con = 0;
		$buy = 0;
		$order = 0;
		$childs = $this->childs($uid);
		$con = count($childs);
		// array_push($childs , $uid);
		if ($childs) {
			// foreach ($childs as $vals) {
			// 	$buy += $this->bus_sum($vals);
			// 	$order += $this->order_sum($vals);
			// }
		}
		return ["con" => $con, "buy" => $buy, "order" => $order];
	}

	/**
	 * 认购数
	 */
	public function order_sum($uid)
	{
		$sum = 0;
		if (!empty($uid)) {
			$where = "";
			$where = "user_id = {$uid} and pay_state in (1,2)";
			$check = Db::name($this->config["order"])->where($where)->count();
			if ($check) {
				$sum =  Db::name($this->config["order"])->where($where)->sum("total_num");
			}
		}
		return $sum;
	}

	/**
	 * 认购数
	 */
	public function bus_sum($uid)
	{
		$sum = 0;
		if (!empty($uid)) {
			$where = "";
			$where = "agid = {$uid} and pay_status = 1 and status = 1";
			$check = Db::name($this->config["buy"])->where($where)->count();
			if ($check) {
				$sum =  Db::name($this->config["buy"])->where($where)->sum("nums");
			}
		}
		return $sum;
	}

	/**
	 * 团队
	 */
	public function childs($uid = 0, &$arr = [])
	{
		$req = Db::name($this->config["user"])->where("pid = " . $uid)->select();
		if ($req) {
			foreach ($req as $key => $val) {
				$arr[] = $val["id"];
				$this->childs($val["id"], $arr);
			}
		}
		return $arr;
	}

	/**
	 * 团队 - 信息
	 */
	public function under_data($uid, $page)
	{
		if (empty($uid)) {
			return false;
		}
		$data = [];
		// $find = Db::name($this->config["user"])
		// ->where("id = " . $uid)
		// ->field("nickname,mobile,from_unixtime(jointime , '%Y-%m-%d %H:%i:%s') times")
		// ->find();
		// $data = [["uid" => $uid, "name" =>  $find["nickname"] , "phone" => $find["mobile"] , "times" => $find["times"]]];
		$limit = 10;
		$offset = ($page - 1) * $limit;
		$where = "pid = {$uid}";
		$under = Db::name($this->config["user"])
			->where($where)
			->field("id,nickname,mobile,from_unixtime(jointime , '%Y-%m-%d %H:%i:%s') times")
			->order("jointime DESC")
			->limit((int) $offset, $limit)
			->select();
		if (count($under)) {
			// $data = $under;
			foreach ($under as $k => $vals) {
				$temp = [];
				$temp["uid"] = $vals["id"];
				$temp["name"] = $vals["nickname"];
				$temp["phone"] = $vals["mobile"];
				$temp["times"] =  $vals["times"];
				array_push($data, $temp);
			}
		}
		return $data;
	}
}
